﻿using Server.DTool;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Server.Struct
{
    class SyncResPool<Item> : ResPool<Item> where Item : class, IPoolItem
    {
        Semaphore _locker;

        public SyncResPool(int capcity, Func<Item> creater) : base(capcity, creater)
        {
            _locker = new Semaphore(1, 1);
        }

        public override Item Take()
        {
            _locker.WaitOne();
            Item item = base.Take();
            _locker.Release();
            return item;
        }

        public override void Recycle(Item t)
        {
            _locker.WaitOne();
            base.Recycle(t);
            _locker.Release();
        }

        public override void RecycleAll(Func<Item, bool> conditdion)
        {
            _locker.WaitOne();
            base.RecycleAll(conditdion);
            _locker.Release();
        }
    }
}
